/*
 * $Id: TaskRelationshipDAO.java,v 1.9 2009/07/10 15:18:36 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.database.dao;

import java.util.ArrayList;
import java.util.List;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.CfgTaskRelationship;
import com.ge.healthcare.autosc.common.database.pojo.CfgTaskType;

/**
 * @author 212042764
 * 
 */
public class TaskRelationshipDAO extends BaseDAO implements
		ITaskRelationshipDAO {
	/**
	 * Eclipse generated
	 */
	private static final long serialVersionUID = -1766965228786790843L;
	private static final Character ALWAYS_CODE_GROUP = Character.valueOf('D');
	private static final Character FAILURE_CODE_GROUP = Character.valueOf('F');
	private static final Character SUCCESS_CODE_GROUP = Character.valueOf('S');

	private static final String TABLE_NAME = "com.ge.healthcare.autosc.common.database.pojo.CfgTaskRelationship";

	private static final String COL_PARENT_ID = "parentId";
	// private static final String COL_CHILD_ID = "childId";

	private ITaskTypeDAO taskTypeDAO = null;

	public TaskRelationshipDAO() {
		super(TABLE_NAME);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#save(java.lang.Object)
	 */
	public void save(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IRequestTypeDAO#saveAlways(java.lang.Object)
	 */
	public void saveAlways(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#deleteByID(java.lang.Object)
	 */
	public void deleteByID(Object pojo) {
		super.deleteByID(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#refresh(java.lang.Object)
	 */
	public void refresh(Object pojo) {
		super.refresh(pojo);
	}

	public List<CfgTaskType> getImmidiateChildren(Long parentId) {
		return getImmidiateChildrenWithCodeGroup(parentId, null);
	}

	public List<CfgTaskType> getImmidiateSuccessPathChildren(Long parentId) {
		return getImmidiateChildrenWithCodeGroup(parentId, SUCCESS_CODE_GROUP);
	}

	public List<CfgTaskType> getImmidiateFailurePathChildren(Long parentId) {
		return getImmidiateChildrenWithCodeGroup(parentId, FAILURE_CODE_GROUP);
	}

	private List<CfgTaskType> getImmidiateChildrenWithCodeGroup(Long parentId,
			Character codeGroup) {
		if (ASCUtil.isNullOrEmptyObject(parentId)) {
			return null;
		}

		List<?> childLst = this.findByProperty(COL_PARENT_ID, parentId);
		List<CfgTaskType> childTaskLst = new ArrayList<CfgTaskType>();
		for (int iCount = 0; iCount < childLst.size(); iCount++) {
			CfgTaskType childTask = taskTypeDAO
					.getByID(((CfgTaskRelationship) childLst.get(iCount))
							.getChildId());
			if (ASCUtil.isNullOrEmptyObject(codeGroup)) {
				childTaskLst.add(childTask);
			} else if (codeGroup.equals(childTask.getCodeGroup())
					|| ALWAYS_CODE_GROUP.equals(childTask.getCodeGroup())) {
				childTaskLst.add(childTask);
			}
		}
		return childTaskLst;

	}

	/**
	 * This method recurses tree and shouldn't be used by anyone other than
	 * dispatcher
	 * 
	 * @param parentId
	 * @return
	 */
	public List<CfgTaskType> getAllSuccessPathChildren(Long parentId) {
		List<CfgTaskType> childTaskLst = new ArrayList<CfgTaskType>();
		List<CfgTaskType> firstChildList = getImmidiateSuccessPathChildren(parentId);
		if (ASCUtil.isNullOrEmptyList(firstChildList)) {
			return null;
		}
		for (int iCount = 0; iCount < firstChildList.size(); iCount++) {
			CfgTaskType child = firstChildList.get(iCount);
			childTaskLst.add(child);
			List<CfgTaskType> subLevelChildTaskLst = getAllSuccessPathChildren(child
					.getTaskTypeId());
			if (!ASCUtil.isNullOrEmptyList(subLevelChildTaskLst)) {
				childTaskLst.addAll(subLevelChildTaskLst);
			}
		}
		return childTaskLst;
	}

	/**
	 * @param taskTypeDAO
	 *            the taskTypeDAO to set
	 */
	public void setTaskTypeDAO(ITaskTypeDAO taskTypeDAO) {
		this.taskTypeDAO = taskTypeDAO;
	}
}
